#include "sound.h"


void Sound::Load(std::string filename)
{
    std::map<std::string, sf::SoundBuffer>::iterator it;

    it = soundMap.find(filename);

    //Sound is not loaded into memory
    if (it == soundMap.end())
    {
        sf::SoundBuffer tempSound;
        tempSound.loadFromFile(filename);
        soundMap[filename] = tempSound;
    }
}

sf::SoundBuffer& Sound::Get(std::string filename)
{
    std::map<std::string, sf::SoundBuffer>::iterator it;

    it = soundMap.find(filename);

    if (it == soundMap.end())
    {
        Sound::Load(filename);
    }

    return soundMap[filename];
}

void Sound::Play(std::string filename)
{
    for (int i = 0; i < MAX_SOUNDS; i++)
    {
        if (sounds[i].getStatus() != sf::Sound::Playing)
        {
            sounds[i].setBuffer(Sound::Get(filename));
            sounds[i].play();
            break;
        }
    }

}

void Music::Play(std::string filename, bool loop)
{
    music.openFromFile(filename);
    music.setLoop(loop);
    music.play();
}
